package org.smartagents.global;

import org.smartagents.body.SubstanceConstants;
import org.smartagents.util.HashUtil;
import org.smartagents.util.Logger;

import java.util.regex.Pattern;

public abstract class Trigerable {

    private String hash = HashUtil.generate10Hash();

    private String linkCondition; //Regular pattern

    private String addBeforeSlot;

    private int maxTick = 5;

    private int tickCount = 0;

    public abstract void trigger(String impulse);

    public abstract void tick();

    public boolean isSatisfied(String condition, String impulse) {

        boolean satisfied = false;
        if (impulse != GlobalConstants.RESET_IMPULSE) {
            if (condition != null && !"".equals(condition)) {
                satisfied = Pattern.matches(condition, impulse);
            }
        } else {
            satisfied = true;
        }
        Logger.logExtreme("Condition : " + condition + " satisfy " + impulse + " = " + satisfied);
        return satisfied;
    }

    public void doTick() {
        if (maxTick == -1) return;

        if (++tickCount > maxTick) {
            tickCount = 0;
            tick();
        }
    }

    public String getHash() {
        return hash;
    }

    public String getLinkCondition() {
        return linkCondition;
    }

    public void setLinkCondition(String linkCondition) {
        this.linkCondition = linkCondition;
    }

    public int getMaxTick() {
        return maxTick;
    }

    public void setMaxTick(int maxTick) {
        this.maxTick = maxTick;
    }

    public String getAddBeforeSlot() {
        return addBeforeSlot;
    }

    public void setAddBeforeSlot(String addBeforeSlot) {
        this.addBeforeSlot = addBeforeSlot;
    }
}
